package com.mt.demo.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisConnectionChecker implements ApplicationRunner {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            // 尝试连接 Redis，执行一个简单的命令（如 PING）
            Object result = redisTemplate.opsForValue().get("testKey");
            if (result == null) {
                redisTemplate.opsForValue().set("testKey", "testValue");
            }
            System.out.println("Redis connection is OK.");
        } catch (Exception e) {
            System.err.println("Redis connection failed: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("Redis connection failed", e);
        }
    }
}
